package com.saodisheng.service.impl;

import com.jfinal.plugin.activerecord.Page;
import com.saodisheng.model.Friend;
import com.saodisheng.model.Posts;
import com.saodisheng.service.AccountService;
import com.saodisheng.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description:
 *
 * @author 扫地生_saodisheng
 * @date 2022/1/29
 */
@Service
public class HomeServiceImpl implements HomeService {
	@Autowired
	private AccountService accountService;

	final Posts postDao = new Posts().dao();
	final Friend friendDao = new Friend().dao();
	final int pageSize = 15;
	
	@Override
	public Page<Posts> getFavorPosts(int userId, int pageNum) {
		String select = "select p.* ";
		String from = "from posts_like pl join posts p on pl.refId=p.id and pl.userId=?";
		
		Page<Posts> postPage = postDao.paginate(pageNum, pageSize, select, from, userId);
		
		accountService.join("userId", postPage.getList(), "nickName", "avatar");
		
		return postPage;
	}

	@Override
	public Page<Friend> getFollows(int userId, int pageNum) {
		String select = "select f.* ";
		String from = "from friend f where f.userId=?";
		
		Page<Friend> followPage = friendDao.paginate(pageNum, pageSize, select, from, userId);
		
		accountService.join("friendId", followPage.getList(), "nickName", "avatar");
		
		return followPage;
	}
	
	@Override
	public Page<Friend> getFans(int userId, int pageNum) {
		String select = "select f.* ";
		String from = "from friend f where f.friendId=?";
		Page<Friend> fansPage = friendDao.paginate(pageNum, pageSize, select, from, userId);

		accountService.join("userId", fansPage.getList(), "nickName", "avatar");
		
		return fansPage;
	}
	
}
